package com.campus.web.controller;


import com.campus.app.mapper.Insert;
import com.campus.app.mapper.Update;
import com.campus.app.service.EventsService;
import com.campus.commons.entity.Event;
import com.campus.commons.entity.User;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/event")
public class EventController {

    @Autowired
    private EventsService eventsService;

    @GetMapping("/")
    public ModelAndView toIndex(ModelMap modelMap, HttpSession session) {
        User user = (User) session.getAttribute("user");
        modelMap.addAttribute("deId", user.getDeid());
        return new ModelAndView("event/index",modelMap);
    }



    @PostMapping("/add")
    @ApiOperation("添加任务")
    public Result addEvent(@Validated(value = Insert.class) Event event) {
        return eventsService.addEvent(event);
    }
    @PostMapping("/getTask")
    @ApiOperation("领取任务")
    public Result getTheTask(@NotNull@Min(1) Integer id,@NotNull@Min(1) Integer studentId) {
        return eventsService.getTheTask(id, studentId);
    }
    @PostMapping("/upState")
    @ApiOperation("后台更新状态")
    public Result updateState(@NotNull@Min(1)Integer id,@NotNull@Min(1) Integer state) {
        return eventsService.updateState(id, state);
    }
    @ApiOperation("撤销任务")
    @PostMapping("/delete")
    public Result deleteEvent(@NotNull@Min(1) Integer id) {
        return eventsService.delEvent(id);
    }

    @PostMapping("/update")
    public Result updateEvent(@Validated(value = Update.class) Event event) {
        return eventsService.updateEvent(event);
    }
    /**
     * 获取某个部门的全部任务
     * @param myPage
     * @param deId
     * @return
     */
    @PostMapping("/all")
    public Result getAllEventsByDeId(@Valid MyPage myPage,@NotNull@Min(1) Integer deId) {
        return eventsService.getAllEvents(myPage, deId);
    }
    @ApiOperation("多条件查询：根据deId，state，studentId")
    @PostMapping("/search")
    public Result getEvents(MyPage<Event> myPage, @Valid Event event) {
        myPage.setData(event);
        return eventsService.getEvents(myPage);
    }



}

